{% extends 'layout.html' %}

{% block conten %}
    <div class="container">
        <div style="margin-bottom: 10px;">
            <input id="btnAdd" type="button" value="添加订单" class="btn btn-success">
        </div>
        <div class="panel panel-default">
            <div class="panel-heading">
             <span class="glyphicon glyphicon-th-list" aria-hidden="true"></span>
             订单列表
         </div>
            <table class="table table-bordered">
          <thead>
            <tr>
              <th>ID</th>
              <th>订单号</th>
              <th>名称</th>
              <th>价格</th>
              <th>状态</th>
              <th>管理员</th>
              <th>操作</th>
            </tr>
          </thead>
          <tbody>
          {% for obj in queryset %}
            <tr>
              <td>{{ obj.id }}</td>
              <td>{{ obj.oid }}</td>
              <td>{{ obj.title }}</td>
              <td>{{ obj.price }}</td>
              <td>{{ obj.get_status_display }}</td>
              <td>{{ obj.admin.username }}</td>
              <td>
                   <input uid="{{ obj.id }}" class="btn btn-primary btn-xs btn-edit" type="button" value="编辑">
                   <input uid="{{ obj.id }}" class="btn btn-danger btn-xs btn-delete" type="button" value="删除">
              </td>
            </tr>
          {% endfor %}
          </tbody>
        </table>

        </div>
            <ul class="pagination">
             {{ page_string }}
            </ul>
    </div>
    <!-- 新建/编辑订单 (对话框) -->
    <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">新建</h4>
          </div>
          <div class="modal-body">

            <form id="formAdd" novalidate>
                <div class="clearfix">
                  {% for field in form %}
                      <div class="col-xs-6">
                          <div class="form-group" style="position: relative; margin-bottom: 20px;">
                              <label>{{ field.label }}</label>
                              {{ field }}
                              <span class="error-msg" style="color: red; position: absolute;"></span>
                          </div>
                       </div>
                  {% endfor %}
                </div>
             </form>

          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
            <button id="btnSave" type="button" class="btn btn-primary">保存</button>
          </div>
        </div>
      </div>
</div>

    <!-- 删除 (对话框) -->
    <div class="modal fade" id="deleteModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="alert alert-danger alert-dismissible fade in" role="alert">

          <h4>您确定要删除吗!</h4>
          <p style="margin: 30px 0;">即将要删除此数据,删除后就找不回来啦.</p>
          <p>
            <button id="btnConfirmDelete" type="button" class="btn btn-danger">确定</button>
            <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
          </p>
        </div>
      </div>
    </div>


{% endblock %}


{% block js %}
    <script type="text/javascript">
        var DELETE_ID;
        var EDIT_ID;

        $(function() {
            bindBtnAddEvent();
            bindBtnSaveEvent();
            bindBtnDeleteEvent();
            bindBtnConfirmDeleteEvent();
            bindBtnEditEvent();
        });


        function bindBtnAddEvent() {

            $("#btnAdd").click(function() {
                // 清空对话框的数据
                $("#formAdd")[0].reset();
                EDIT_ID=undefined;
                $('#myModalLabel').text("新建");
                // 点击新建按钮, 显示对话框
                $('#myModal').modal('show');
            })
        }

        function bindBtnSaveEvent() {
            $('#btnSave').click(function(){
                // 清除所有错误信息
               $('.error-msg').empty();

                if(EDIT_ID){
                    // 编辑
                    doEdit();
                }else{
                    doAdd();
                }
            })
        }

        function doAdd(){
            // 添加
            $.ajax({
                   url: '/order/add/',
                   type: 'post',
                   data:$('#formAdd').serialize(),
                   dataType:'JSON',
                   success:function(res){
                       if(res.status){
                           // 清空表单
                           $('#formAdd')[0].reset();
                           // 关闭对话框
                            $('#myModal').modal('hide');
                           // 刷新页面
                           location.reload()
                       }else{
                           //把错误信息放在对话框中
                           $.each(res.error, function(name,errorList){
                               $("#id_"+name).next().text(errorList[0]);
                           })
                       }
                   }
               })
        }

        function doEdit(){
            // 编辑
             $.ajax({
                   url: '/order/edit/'+'?uid='+EDIT_ID,
                   type: 'post',
                   data:$('#formAdd').serialize(),
                   dataType:'JSON',
                   success:function(res){
                       if(res.status){
                           // 清空表单
                           $('#formAdd')[0].reset();
                           // 关闭对话框
                            $('#myModal').modal('hide');
                           // 刷新页面
                           location.reload()
                       }else{
                           if(res.tips){
                               alert(res.tips);
                           }else {
                               //把错误信息放在对话框中
                                $.each(res.error, function(name,errorList){
                               $("#id_"+name).next().text(errorList[0]);
                           })
                           }
                       }
                   }
               })
        }





        function bindBtnDeleteEvent() {
            $(".btn-delete").click(function(){
               $("#deleteModal").modal('show');
                  // 获取当前行的ID赋值给变量
                DELETE_ID = $(this).attr("uid");
            })
        }

        function bindBtnConfirmDeleteEvent(){
            $("#btnConfirmDelete").click(function(){
                // 点击确认删除按钮,将全景变量中设置的Id发送到后台
                $.ajax({
                    url:"/order/delete/",
                    type:"GET",
                    data:{
                        uid:DELETE_ID
                    },
                    dataType: "JSON",
                    success: function(res){
                        if (res.status){
                            {#alert("删除成功!");#}
                            {#$('#deleteModal').modal('hide');#}
                            location.reload();
                        }else{
                            // 删除失败
                            alert(res.error)
                        }
                    }
                })
            });
        }

        function bindBtnEditEvent() {
            $(".btn-edit").click(function(){

                var uid = $(this).attr("uid");
                 //发送ajax去后台获取当前行的相关数据
                EDIT_ID = uid;

                $.ajax({
                    url:'/order/detail/',
                    type:"get",
                    data:{
                        uid:uid
                    },
                    dataType:"JSON",
                    success:function(res){
                        if (res.status) {
                            $.each(res.data, function(name,vaule){
                                $("#id_"+name).val(vaule);
                            })
                            //修改对话框标题
                            $('#myModalLabel').text("编辑");
                             $('#myModal').modal('show');
                        }else {
                            alert(res.error)
                        }
                    }
                })
            })
        }





    </script>
{% endblock %}